#include "led.h"
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiManager.h>
//#include <Ticker.h>

#define F_light 5
//Ticker battery;
int battery1, battery2 = 0;

WiFiManager wifiManager;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

const char* mqttServer = "1.14.32.185";
const char* topic1 = "4Gcar/Led";

int LED_MODLE = LED_OFF;

void setup() {
  Serial.begin(115200);
  pinMode(F_light, OUTPUT);
  digitalWrite(F_light, 1);

  wifiManager.autoConnect("4G Car Led");

  Led_Init();

  mqttClient.setServer(mqttServer, 1883);
  mqttClient.setCallback(receiveCallback);
  connectMQTTserver();

  ArduinoOTA.setHostname("4G car led");
  ArduinoOTA.setPassword("88888888");
  ArduinoOTA.begin();

  //battery.attach(2, public_battery);
}

void loop() {
  Led_Control(LED_MODLE);
  if (mqttClient.connected()) {  // 如果开发板成功连接服务器
    mqttClient.loop();           // 处理信息以及心跳
  } else {                       // 如果开发板未能成功连接服务器
    connectMQTTserver();         // 则尝试连接服务器
  }

  ArduinoOTA.handle();
}

void connectMQTTserver() {
  String clientId = "Car_Led_Control";

  // 连接MQTT服务器
  if (mqttClient.connect(clientId.c_str())) {
    subscribeTopic();  // 订阅指定主题
  } else {
    Serial.print("MQTT Server Connect Failed. Client State:");
    Serial.println(mqttClient.state());
    delay(5000);
  }
}

void subscribeTopic() {
  String topicString = "4Gcar/Led";
  char subTopic[topicString.length() + 1];
  strcpy(subTopic, topicString.c_str());
  // 通过串口监视器输出是否成功订阅主题以及订阅的主题名称
  if (mqttClient.subscribe(subTopic)) {
    //Serial.println("Subscrib Topic success!");
  } else {
    Serial.print("Subscribe Fail...");
  }
}

// 收到信息后的回调函数
void receiveCallback(char* topic, byte* payload, unsigned int length) {
  if (strcmp(topic, topic1) == 0) {
    Switch_LED((char)payload[0]);
  }
}

void Switch_LED(char order) {
  switch (order) {
    case '0':
      {
        if (digitalRead(F_light) == 0) {
          digitalWrite(F_light, 1);
        } else {
          digitalWrite(F_light, 0);
        }
        break;
      }
    case '1':
      {
        LED_MODLE = LED_BLUE;
        break;
      }
    case '2':
      {
        LED_MODLE = LED_ON;
        break;
      }
    case '3':
      {
        LED_MODLE = LED_TIME_CORRECTION;
        break;
      }
    case '4':
      {
        LED_MODLE = LED_OFF;
        break;
      }
  }
}

// void public_battery() {
//   // 建立发布主题
//   String topicString = "4GCar/battery";
//   char publishTopic[topicString.length() + 1];
//   strcpy(publishTopic, topicString.c_str());

//   char temp[2] = {};
//   battery1 = analogRead(A0);
//   Serial.print("AO口:");
//   Serial.println(battery1);
//   if (battery1 >= 680 && battery1 <= 855) {
//     battery2 = (battery1 - 680) / 1.75;
//     Serial.println(battery2);
//   } else {
//     Serial.println("chaochufanwei");
//   }




//   strcpy(temp, String(battery2).c_str());

//   // 实现ESP8266向主题发布信息
//   if (mqttClient.publish(publishTopic, temp)) {
//   } else {
//     Serial.println("Message Publish Failed.");
//   }
// }